﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace tiDE.Model.Projects
{
    [Serializable]
    public class Solution
    {
        #region Constructors

        public Solution()
        {
            this.DefaultDebuggerProject = null;
            this.Name = null;
            this.OpenFiles = new List<string>();
            this.Projects = new List<Project>();
            this.SolutionFile = null;
        }

        public Solution(string File)
        {
            this.DefaultDebuggerProject = null;
            this.Name = null;
            this.OpenFiles = new List<string>();
            this.Projects = new List<Project>();
            this.SolutionFile = File;
            this.Save();
        }

        #endregion

        #region Properties

        /// <summary>
        /// The friendly name of this solution
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// This solution's .tisln file
        /// </summary>
        public string SolutionFile { get; set; }

        /// <summary>
        /// Projects associaited with this solution
        /// </summary>
        public List<Project> Projects { get; set; }

        /// <summary>
        /// All files open for editing in this solution
        /// </summary>
        public List<string> OpenFiles { get; set; }

        /// <summary>
        /// The project loaded for debugging when this
        /// solution is debugged as a whole.
        /// </summary>
        public Project DefaultDebuggerProject { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Loads a solution object from the given
        /// file stream.
        /// </summary>
        public static Solution Load(Stream File)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Solution));
            return (Solution)xmlSerializer.Deserialize(File);
        }

        /// <summary>
        /// Saves this solution to Solution.SolutionFile
        /// </summary>
        public void Save()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Solution));
            Stream s = File.Open(SolutionFile, FileMode.OpenOrCreate);
            xmlSerializer.Serialize(s, this);
            s.Close();
        }

        #endregion
    }
}
